import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma.service';
import { VideoInfo, Prisma } from '@prisma/client';
import { PageResponse } from 'src/app.controller';

@Injectable()
export class VideoInfoService {
  constructor(private prisma: PrismaService) {}

  async videoInfo(
    videoInfoWhereUniqueInput: Prisma.VideoInfoWhereUniqueInput,
  ): Promise<VideoInfo | null> {
    return this.prisma.videoInfo.findUnique({
      where: videoInfoWhereUniqueInput,
      include: { records: true },
    });
  }

  async videoInfos(params: {
    skip?: number;
    take?: number;
    cursor?: Prisma.VideoInfoWhereUniqueInput;
    where?: Prisma.VideoInfoWhereInput;
    orderBy?: Prisma.VideoInfoOrderByWithRelationInput;
  }): Promise<PageResponse<VideoInfo>> {
    const { skip, take, cursor, where, orderBy } = params;

    const total = await this.prisma.videoInfo.count();

    const data = await this.prisma.videoInfo.findMany({
      skip,
      take,
      cursor,
      where,
      orderBy,
      include: {
        records: true,
      },
    });
    console.log('data', data);
    return Promise.resolve({
      data,
      total,
      current: skip / take,
      pageSize: take,
      success: true,
    });
  }

  async createVideoInfo(data: Prisma.VideoInfoCreateInput): Promise<VideoInfo> {
    return this.prisma.videoInfo.create({
      data,
    });
  }

  async updateVideoInfo(params: {
    where: Prisma.VideoInfoWhereUniqueInput;
    data: Prisma.VideoInfoUpdateInput;
  }): Promise<VideoInfo> {
    const { where, data } = params;
    return this.prisma.videoInfo.update({
      data,
      where,
    });
  }

  async deleteVideoInfo(
    where: Prisma.VideoInfoWhereUniqueInput,
  ): Promise<VideoInfo> {
    return this.prisma.videoInfo.delete({
      where,
    });
  }
}
